const ipUtil = require("../../utils/hostUtil");
const requestUtil = require("../../utils/requestUtil");
Page({
    data: {
        list: ['全部', '待付款', '待发货', '待收货','待评价'],
        clickNumber: 0,
        data: [],
        currentHeight: 358, //当前的swiper-item或者 scroll-view更改时 动态更改 否则显示不正常 单个view 348+10 rpx
    },
    updateSelectedPage: function (current) {
        var itemWidth = this.data.itemWidth;//一个标签的宽度
        var height = 0;
        var order_list = [];
        var activeWidth = (current - 3) * itemWidth;//重点：重新定义固定轮播点（指定哪个位置轮播）
        // 根据列表中的个数确定height
        if (current == 0) {
            order_list = this.data.allData;
        } else if (this.data.data.length > 0) {
            var data = this.data.data;
            order_list = data[current - 1] || [];
        }
        height = this.total_height(order_list || [])
        this.setData({
            currentHeight: height || 0,
            navScrollLeft: activeWidth,
            clickNumber: current,
        })
    }, onShow: function () {
        this.loadPage(this.data.clickNumber);
    }
    ,
    //点击上方文字  切换
    onCenterTap: function (event) {
        var current = event.target.dataset.current;
        this.updateSelectedPage(current);
    },

    // 滚动切换标签样式
    onChangeSwipeTap: function (event) {
        var current = event.detail.current;
        this.updateSelectedPage(current);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.showLoading({
            title: '加载中',
        })
        var type = options.type || ""; // 通过哪个菜单点进来的 切换到对应的订单详情下
        var current = this.getCurrent(type);
        wx.setNavigationBarTitle({title: '订单'});
        var height = wx.getSystemInfoSync().windowHeight;
        var width = wx.getSystemInfoSync().windowWidth;
        var itemWidth = width * 0.172 * 0.88;//对应wxss
        this.setData({
            height: height,
            width: width,
            itemWidth: itemWidth
        })
        this.loadPage(current);
        this.updateSelectedPage(current);
    },
    /**
     * 请求订单数据
     */
    loadPage: function (current) {
        var that = this;
        wx.request({
            url: `${ipUtil.host}${requestUtil.all_order}`,
            data: {token: wx.getStorageSync("token")},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                console.log(res)
                if (res.data) {
                    var data = [];
                    var data1 = res.data;
                    var allData = [];
                    data.push(data1['0']);
                    data.push(data1['1']);
                    data.push(data1['2']);
					data.push(data1['3'])
                    if (data1['0']) {
                        data1['0'].forEach(item => {
                            allData.push(item);
                        })
                    }
                    if (data1['1']) {
                        data1['1'].forEach(item => {
                            allData.push(item);
                        })
                    }
                    if (data1['2']) {
                        data1['2'].forEach(item => {
                            allData.push(item);
                        })
                    }
                    if (data1['3']) {
                        data1['3'].forEach(item => {
                            allData.push(item);
                        })
						console.log(allData)
                    }
                    if (data1['-1']) {
                        data1['-1'].forEach(item => {
                            allData.push(item);
                        })
                    }
                    that.setData({
                        data: data,
                        allData: allData
                    })
                }
                var height = that.total_height(current == 0 ? (allData || []) : (data[current - 1] || []))
                that.setData({
                    currentHeight: height
                })
            }
        })
        wx.hideLoading();
    },
    getCurrent: function (type) {
        switch (type) {
            case "pay":
                return 1;
            case "ship":
                return 2;
            case "receipt":
                return 3;
            case "assess":
                return 4;
            default:
                return 0;
        }
    },
    /**
     *
     * @param e
     */
    to_detail: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/pages/order/order-detail?orderId=${id}`
        });
    },
    // addToR:function(e){
    //     var id = e.currentTarget.dataset.id;
    //     console.log(id)
    //     wx.navigateTo({
    //       url: '../return/return?id=' + id,
    //     })
    // },
    /**
     * totalHeight
     */
    total_height: function (arr) {
        var order_vo_list_count = 0;
        var height = arr.length * 410;
        arr.forEach(item => {
            order_vo_list_count += item.orderVoList == null ? 0 : item.orderVoList.length;
        });
        height += (order_vo_list_count - arr.length) * 126 * 2;
        return height;
    },
    confirmOrder: function (e) {
        var orderId = e.currentTarget.dataset.id;
        var that = this;
        wx.request({
            url: `${ipUtil.host}${requestUtil.sign}`,
            data: {orderId: orderId},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                if (res.data.code == 0) {
                    wx.showToast({
                        title: "收货成功",
                        icon: "none",
                        duration: 2000
                    })
                    that.loadPage(that.data.clickNumber);
                }
            }
        })
    },
	rate:function(e){
		var id = e.currentTarget.dataset.id;
		console.log(id)
		wx.navigateTo({
		  url: '../rate/rate?id=' + id,
		})
	},
    ship_state:function (){
        wx.showToast({title: "功能正在开发...", icon: "none", duration: 2000})
        return;
    },
    rebuy: function () {
        wx.showToast({title: '功能正在开发...', icon: 'none', duration: 2000})
        return;
    },
});